package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.TbCommunityMapper;
import com.hqyj.entity.SearchBean;
import com.hqyj.pojo.TbBuilding;
import com.hqyj.dao.TbBuildingMapper;
import com.hqyj.pojo.TbHouse;
import com.hqyj.service.TbBuildingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 楼栋表 服务实现类
 * </p>
 *
 * @author gp
 * @since 2021-09-25
 */
@Service
public class TbBuildingServiceImpl implements TbBuildingService {
    @Autowired(required = false)
    TbBuildingMapper tbBuildingMapper;

    @Autowired
    TbCommunityMapper tbCommunityMapper;

    @Override
    public HashMap<String, Object> List(TbBuilding building) {
        HashMap<String ,Object> map = new HashMap<>();
        Page<TbBuilding> TbBuildingPage = new Page<>(building.getPage(),building.getRow());
        //创建查询条件对象
        QueryWrapper<TbBuilding> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(
                building.getId()!=null&&!building.getId().equals(""),
                "id",
                building.getId());
        //查询分页，获取分页数据集
        Page<TbBuilding> pageList = tbBuildingMapper.selectPage(TbBuildingPage,queryWrapper);
        //获取当前页的集合
        map.put("info",pageList.getRecords());
        //获取总条数
        map.put("total",pageList.getTotal());
        //获得总页数
        map.put("totalPage",pageList.getPages());
        return map;
    }

    @Override
    public HashMap<String, Object> insert(TbBuilding building) {
        HashMap<String ,Object> map = new HashMap<>();

        Integer communityId = building.getCommunityId();
        String communityName = tbCommunityMapper.getNameById(communityId);
        building.setCommunityName(communityName);
        int num = tbBuildingMapper.insert(building);
        if (num>0){
            map.put("info","添加成功");
        }else {
            map.put("info","添加失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(TbBuilding building) {
        HashMap<String ,Object> map = new HashMap<>();
        int num = tbBuildingMapper.updateById(building);
        if (num>0){
            map.put("info","修改成功");
        }else {
            map.put("info","修改失败");
        }
        return map;

    }

    @Override
    public HashMap<String, Object> delete(Integer id) {
        HashMap<String ,Object> map = new HashMap<>();
        int num = tbBuildingMapper.deleteById(id);
        if (num>0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> queryByName(String name) {
        HashMap<String ,Object> map = new HashMap<>();
        List<TbBuilding> tbBuildings = tbBuildingMapper.queryByName(name);
        map.put("info",tbBuildings);
        return map;
    }

    @Override
    public HashMap<String, Object> listPage(SearchBean searchBean) {
        HashMap<String, Object> map = new HashMap<>();

        // 第一步：创建分页对象
        Page<TbBuilding> tbBuildingPage = new Page<>(searchBean.getCurrentPage(),searchBean.getPageSize());

        // 创建查询条件对象
        QueryWrapper<TbBuilding> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(searchBean.getDirection() != null && !searchBean.getDirection().equals(""), "community_name", searchBean.getDirection());

        // 第二步：查询分页，获取分页数据集
        Page<TbBuilding> tbBuildingList = tbBuildingMapper.selectPage(tbBuildingPage, queryWrapper);
        // 获取当前页集合
        map.put("info", tbBuildingList.getRecords());
        // 获取总条数
        map.put("total", tbBuildingList.getTotal());
        // 获取总页数
        map.put("totalPage", tbBuildingList.getPages());

        return map;
    }
}
